<?php
/**
 * Created by PhpStorm.
 * User: lzy 林子彦
 * Date: 2018/6/1
 * Time: 上午1:09
 */

namespace App\Admin\Controllers;

use App\Admin\Contracts\AuthRuleInterface;
use App\Admin\Contracts\MenuInterface;
use App\Base\Controllers\Controller;
use Illuminate\Http\Request;

/**
 * 用户控制类
 * Class MenuController
 * @package App\Admin\Controllers
 */
class AuthManagerController extends Controller
{

    /**
     * 权限接口
     * @return AuthRuleInterface
     */
    public function getAuthRuleInterface()
    {
        return app()->make(AuthRuleInterface::class);
    }

    /**
     * 菜单接口
     * @return MenuInterface
     */
    public function getMenuInterface()
    {
        return app()->make(MenuInterface::class);
    }

    public function index(Request $request)
    {

        if ($request->getMethod() == "POST") {
            return $this->getAuthRuleInterface()->getAuthGroupList([]);
        }

        return view("admin.auth.index");
    }

    /**
     * 权限组添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->getMethod() == "POST") {
            return $this->getAuthRuleInterface()->updateOrCreateAuthGroup($request->all());
        }
        return view("admin.auth.add");
    }

    /**
     * 权限组编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        if ($request->getMethod() == "POST") {
            return $this->getAuthRuleInterface()->updateOrCreateAuthGroup($request->all());
        }
        $id = $request->input("id");
        $info = $this->getAuthRuleInterface()->getAuthGroupInfo(["id" => $id]);
        return view("admin.auth.edit")->with("info", $info);
    }

    public function del(Request $request){
        $id=$request->input("id");
        if ($request->getMethod() == "POST") {
            return $this->getAuthRuleInterface()->delAuthGroup(["id"=>$id]);
        }
    }

    /**
     * 先择菜单
     * @param Request $request
     * @return $this
     */
    public function menu(Request $request)
    {
        $id = $request->input("id");
        if ($request->getMethod() == "POST") {
            $data = $request->input("data");
            if (empty($data)) {
                $data = array();
            }
            return $this->getAuthRuleInterface()->updateAuthGroup(["id" => $id], ["rules" => implode(",", $data)]);
        }
        $this->updateNodeByMenu();  //更新权限菜单表
        $menuList = $this->getMenuInterface()->getMenuList([]); //获取菜单
        //获取id 权限组
        $groupInfo = $this->getAuthRuleInterface()->getAuthGroupInfo(["id" => $id]);
        //权限组则
        $own_rose_rule_arr = array();
        if (!empty($groupInfo->rules)) {
            $own_rose_rule_arr = explode(",", $groupInfo->rules);
        }
        //获取权限组菜单列表
        $ruleList = $this->getAuthRuleInterface()->getAuthRuleListNameByArray(["module" => 'admin']);
        //生成新数组
        $new_menus = array();
        foreach ($menuList as $k => $item) {
            $new_row = array();
            if ($item['parentid'] == 0) {
                $new_row['name'] = '顶级节点';
            }
            $new_row["id"] = $item->id;
            $new_row["name"] = $item->title;
            $new_row["pId"] = $item->pid;
            $new_row["icon"] = $item->icon;
            //判断对应的后台id
            $f_url = strtolower(MODULE_NAME . '/' . $item->url);
            $new_row["rule_id"] = $ruleList[$f_url];
            if (in_array($new_row["rule_id"], $own_rose_rule_arr)) {
                $new_row["checked"] = true;
            }
            $new_menus[] = $new_row;


            //合并起权限组菜单
        }
        if (!empty($own_rose_rule_arr)) {
            $new_menus[] = array(
                'id' => 0,
                'rule_id' => 0,
                'name' => '顶级权限',
                'checked' => true
            );
        } else {
            $new_menus[] = array(
                'id' => 0,
                'rule_id' => 0,
                'name' => '顶级权限',
            );
        }


//       $menuArr=array();
//       foreach ( $menuList as $k=> $item) {
//           $menuArr[$k]["id"]=$item->id;
//           $menuArr[$k]["name"]=$item->title;
//           $menuArr[$k]["pId"]=$item->pid;
//           $menuArr[$k]["icon"]=$item->icon;
//       }
        return view("admin.auth.checkbox_meun")->with("menu_list", json_encode($new_menus))->with("id", $id);
    }


    /**
     * 更新角色表
     */
    private function updateNodeByMenu()
    {
        $this->getAuthRuleInterface()->updateNodeByMenu();
    }


}